/**
 *导航栏文件
 */
'use strict';
import React , { Component } from 'react';
import { Link } from 'react-router';
import { Menu , Breadcrumb , Icon } from 'antd';
import NavList from './NavList.jsx';
import './Navigation.css';
const SubMenu = Menu.SubMenu;
const menus = NavList.reduce((arr, current) => arr.concat(current.children), []);
let title = '';
let pathname = '';

//获取导航栏标题的值
function getTitle(data,name){
	if (!data) {
		return null;
	}
	return data.map((item) => {
		if (!item.name) {
			return null;
		}
		if(item.path === name || item.info === name){
			title = item.component;
			pathname = item.path;
			return null;
		}
		getTitle(item.children,name);
	});
}

export default class Navigation extends Component {	  
	constructor(props) {
  	super(props);
		
		this.pathname = this.props.pathname || '';
		getTitle(menus,this.pathname);
		this.state = {
			openKeys:[title],
			pathname:pathname
		};
	}
   componentWillReceiveProps(props) {
		 getTitle(menus,props.pathname);
			 this.setState({
				 pathname:pathname,
				 openKeys:[title]
			 });
	 }
	//导航栏折叠栏更改时执行，用于只打开一个导航栏折叠
	handleOpenChange = (openKeys) => {
		const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1);
		this.setState({
    	openKeys: latestOpenKey ? [latestOpenKey] : []
    });
	}
  
  //点击用户角色管理导航，其他收缩
  selectUser = (item, key, selectedKeys) => {
   item.key === '/Rankings' ? this.setState({openKeys: ['排行榜']}) : key = '';
  }
	
	//获取导航栏目录名字信息，输出DOM目录
	getNavMenuItems = (menusData) => {
		let userPosition = localStorage.getItem('userPosition');
		if (!menusData) {
		  return [];
		}
		return menusData.map((item) => {
		  if (!item.name) {
				return null;
		  }
		  let itemPath;
		  if (item.path.indexOf('http') === 0) {
				itemPath = item.path;
		  } else {
				itemPath = `${item.path || ''}`.replace(/\/+/g, '/');
		  }
			let active = [];
			if((userPosition === '2' || userPosition === '5' || userPosition === '6') && (item.name === '财务管理' || item.name === '新建项目' || item.name === '人员注册')){
return '';
			}else if((userPosition === '3') && (item.name === '财务管理' || item.name === '人员注册')){
return '';
			}else if((userPosition === '4') && (item.name === '新建项目' || item.name === '人员注册')){
					return '';
			}else{}
			if (item.children && item.children.some(child => child.name)) {
				return (
					<SubMenu
						title = {
							item.icon ? (
							<span>
								<Icon type = {item.icon}/>
								<span>{item.name}</span>
							</span>
							) : item.name
						}
						key = {item.path || item.key}
					>
						{this.getNavMenuItems(item.children)}
					</SubMenu>
				);
		  }
		  		 
		  return (
				<Menu.Item key = {item.key || item.path}>
					<Link to = {itemPath} target = {item.target} onClick = {this.props.onClick}>
						{item.icon ? <Icon type = {item.icon}/> : null}
						<span>{item.name}</span>
					</Link>
				</Menu.Item>
		  );
		});
	}

	render() {
    return (
			<Menu theme = 'light'  mode = 'inline' 
			  openKeys = {this.state.openKeys}
        onOpenChange = {this.handleOpenChange}
				selectedKeys = {[this.state.pathname]}
        onSelect = {this.selectUser}			
      >
			 	{this.getNavMenuItems(menus)}
		  </Menu>
		);
	}
}